﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using FarmerJoe.ClassSettings;
using FarmerJoe.Settings;
using Styx;

namespace FarmerJoe
{
    public partial class ucHunterSetting : UserControl
    {
         private bool _isLoading = true;

                
        public ucHunterSetting()
        {
            InitializeComponent();
            PopulateUIControls();

            switch (StyxWoW.Me.Specialization)
            {
                case WoWSpec.HunterBeastMastery:
                    tabControl1.SelectTab("Beast");
                    break;

                case WoWSpec.HunterSurvival:
                    tabControl1.SelectTab("Survival");
                    break;

                case WoWSpec.HunterMarksmanship:
                    tabControl1.SelectTab("Marksmanship");
                    break;

            }

            _isLoading = false;
        }


        private void PopulateUIControls()
        {
            SuspendLayout();

            Type type = typeof(HunterSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties)
            {
                PropertyInfo pInfo = type.GetProperty(setting.Name);
                object propValue = pInfo.GetValue(FpswareSettings.Instance.Hunter, null);

                foreach (GroupBox gb in from TabPage tab in tabControl1.Controls from GroupBox gb in tab.Controls select gb)
                    foreach (object obj in gb.Controls)
                    {
                        // Populate all combo boxes, this will be the most common
                        if (obj is ucDropdown)
                        {
                            var cbox = (ucDropdown) obj;

                            cbox.ValueChanged += ucControl_ValueChanged;
                            if (cbox.SettingName == setting.Name && cbox.Value != propValue.ToString())
                            {
                                cbox.Value = propValue.ToString();
                                break;
                            }
                        }

                            // Populate all progress bars, mostly for health or mana 
                        else if (obj is ucProgressBar)
                        {
                            var pbar = (ucProgressBar) obj;

                            pbar.ValueChanged += ucControl_ValueChanged;

                            if (pbar.SettingName == pInfo.Name)
                            {
                                pbar.Value = (int) propValue;
                                break;
                            }
                        }
                    }
            }

            _isLoading = false;
            ResumeLayout();
        }

       
        private void ucControl_ValueChanged(object sender, EventArgs e)
        {
            // Basically enumerate the [Druid] setting class and find the Property that matches the name of 'e.SettingName' and assign the value of 'e.Value'
            // This saves us creating code for every item / setting and adds reusability

            if (_isLoading) return;

            Type type = typeof(HunterSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties.Where(setting => e.SettingName == setting.Name))
            {
                setting.SetValue(FpswareSettings.Instance.Hunter, e.Value, null);
            }
        }

        private void ucControl_ValueChanged(object sender, cbEventArgs e)
        {
            // Basically enumerate the [Druid] setting class and find the Property that matches the name of 'e.SettingName' and assign the value of 'e.Value'
            // This saves us creating code for every item / setting and adds reusability

            if (_isLoading) return;

            Type type = typeof(HunterSettings); PropertyInfo[] settingsProperties = type.GetProperties();
            foreach (PropertyInfo setting in settingsProperties.Where(setting => e.SettingName == setting.Name))
            {
                setting.SetValue(FpswareSettings.Instance.Hunter, e.Value, null);
            }
        }

        private void ucWyvernSting_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("When", "BeastWyvernStingWhen", new List<string> { "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only on 5+ adds"}, "", "");
            frm2.AddDropDown("Pet Has Aggro", "BeastWyvernStingPetAggro", new List<string> { "... ignore target", "... consider target" }, "Wyvern Sting", "If the mob is aggroed on the pet either ignore it or consider it");
            
            //frm2.AddProgressBar("Health", "AfflictionDarkBarginHealth", 0, 100, "Dark Bargin", "The health percent at which you will cast Dark Bargin");
            //frm2.AddDropDown("Malefic Grasp", "AfflictionPullMaleficGrasp", new List<string> { "... always", "... never" }, "", "");

            frm2.FormHeading = "Wyvern Sting";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: When you have 3+ adds [When] cast Wyvern Sting. If the mob is attacking the pet [Pet Has Aggro] ignore it.";

            frm2.Show(this);
        }

        private void ucScatterShot_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("When", "BeastScatterShotWhen", new List<string> { "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only on 5+ adds" }, "", "");
            frm2.AddDropDown("Pet Has Aggro", "BeastScatterShotPetAggro", new List<string> { "... ignore target", "... consider target" }, "Scatter Shot", "If the mob is aggroed on the pet either ignore it or consider it");

            frm2.FormHeading = "Scatter Shot";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: When you have 3+ adds [When] cast Scatter Shot. If the mob is attacking the pet [Pet Has Aggro] ignore it.";

            frm2.Show(this);
        }

        private void ucFreezingTrap_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("When", "BeastFreezingTrapWhen", new List<string> { "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only on 5+ adds" }, "", "");
            frm2.AddDropDown("Pet Has Aggro", "BeastFreezingTrapPetAggro", new List<string> { "... ignore target", "... consider target" }, "Freezing Trap", "If the mob is aggroed on the pet either ignore it or consider it");

            frm2.FormHeading = "Freezing Trap";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: When you have 3+ adds [When] cast Freezing Trap. If the mob is attacking the pet [Pet Has Aggro] ignore it.";

            frm2.Show(this);
        }

        private void ucSerpentSting_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("DoT Adds", "SerpentStingAdds", new List<string> { "... always", "... never"}, "", "");

            frm2.FormHeading = "Serpent Sting Adds";
            frm2.lblDescription.Visible = false;
            frm2.lblDescription.Text = "For example: ";

            frm2.Show(this);
        }

        private void ucFeignDeath_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Health", "FeignDeathHealth", 0, 100, "", "");
            frm2.AddDropDown("Disengage", "FeignDeathDisengage", new List<string> { "... always", "... never" }, "Feign Death", "Should Disengage be used before casting Feign Death");

            frm2.FormHeading = "Feign Death";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "For example: Cast Feign Death when your heal is below 40% [Health] but use Disengage before doing so.";

            frm2.Show(this);
        }

        private void ucPetSpecialAbilities_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Cower Health", "PetAbilityCowerHealth", 0, 100, "", "");
            frm2.AddDropDown("Cower When", "PetAbilityCowerWhen", new List<string> { "... always", "... never", "... only on adds", "... only on 3+ adds", "... only on 4+ adds" }, "", "");
            frm2.AddProgressBar("Last Stand Health", "PetAbilityLastStandHealth", 0, 100, "", "");
            frm2.AddDropDown("Rabid", "PetAbilityRabid", new List<string> { "... always","... never", "... only on adds", "... only on 3+ adds", "... only on 4+ adds" }, "", "");
            frm2.AddDropDown("Roar of Sacrifice", "PetAbilityRoarOfSacrifice", new List<string> { "... always", "... never", "... only on adds", "... only on 3+ adds", "... only on 4+ adds", "... only if target is player" }, "", "");

            frm2.FormHeading = "Pet Special Abilities";
            frm2.lblDescription.Visible = false;
            frm2.lblDescription.Text = "For example: Cast Feign Death when your heal is below 40% [Health] but use Disengage before doing so.";

            frm2.Show(this);
        }

        private void ucPowerShot_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddProgressBar("Health", "PowershotHealth", 0, 100, "", "");

            frm2.FormHeading = "Powershot";
            frm2.lblDescription.Visible = false;
            frm2.lblDescription.Text = "For example: Only cast Powershot if the target's health is above 80% [Health]";

            frm2.Show(this);
        }

        private void ucReadiness_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Exhilaration on CD", "ReadinessExhilaration", new List<string> { "... always", "... never"}, "Readiness", "Cast Readiness if Exhilaration is on cooldown");
            frm2.AddDropDown("Rapid Fire on CD", "ReadinessRapidFire", new List<string> { "... always", "... never" }, "Readiness", "Cast Readiness if Rapid Fire is on cooldown");

            frm2.FormHeading = "Readiness";
            frm2.lblDescription.Visible = false;
            frm2.lblDescription.Text = "For example: Cast Feign Death when your heal is below 40% [Health] but use Disengage before doing so.";

            frm2.Show(this);
        }

        private void ucAspectOfTheCheetah_AdditionalsButtonPressed(object sender, System.ComponentModel.AddingNewEventArgs e)
        {
            uiExtendedOptions frm2 = new uiExtendedOptions();
            frm2.ShowLogicProcess = false;
            frm2.AddDropDown("Min Distance", "AspectOfTheCheetahMinDistance", new List<string> { "5", "10", "15", "20", "25", "30", "35", "40" }, "", "");

            frm2.FormHeading = "Aspect of the Cheetah";
            frm2.lblDescription.Visible = true;
            frm2.lblDescription.Text = "The minimum distance of the POI to consider using AotC";

            frm2.Show(this);
        }
    }
}
